package com.test.controller;

import com.test.entity.Const;
import com.test.entity.RestBean;
import com.test.entity.dto.Interact;
import com.test.entity.dto.Type;
import com.test.entity.vo.request.CommentVo;
import com.test.entity.vo.request.TopicCreateVo;
import com.test.entity.vo.request.UpdateTopicVo;
import com.test.entity.vo.response.*;
import com.test.service.CommentService;
import com.test.service.TopicService;
import com.test.service.TypeService;
import com.test.service.WeatherService;
import com.test.utils.ControllerUtils;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("api/forum")
public class ForumController {
    @Resource
    CommentService commentService;
    @Resource
    WeatherService weatherService;
    @Resource
    TypeService typeService;
    @Resource
    TopicService topicService;
    @Resource
    ControllerUtils utils;
    @GetMapping("/weather")
    public RestBean<WeatherVo> getWeather(double latitude, double longitude){
        WeatherVo weatherVo = weatherService.fetchWeather(latitude,longitude);
        return weatherVo == null ? RestBean.failure(400,"获取天气失败") : RestBean.success(weatherVo);
    }
    @GetMapping("/types")
    public RestBean<List<Type>> getTypes(){
        return RestBean.success(typeService.getTypeList());

    }
    @PostMapping("/create-topic")
    public RestBean<Void> createTopic(@RequestBody TopicCreateVo vo,
                                      @RequestAttribute(Const.VERIFY_USER_ID) int id){
         return utils.handleMessage(() -> (topicService.createTopic(id,vo)));


    }
    @GetMapping("/list-topic")
    public RestBean<List<TopicPreviewVo>> getListTopic(@RequestParam @Min(0) int type,
                                                       @RequestParam @Min(0) int page){
        return RestBean.success(topicService.getListTopic(type,page+1));
        }
    @GetMapping("/top-topic")
    public RestBean<List<TopTopicVo>> getTopTopic(){
        return RestBean.success(topicService.getTopTopic());

    }
    @GetMapping("/topic")
    public RestBean<TopicDetailVo> getTopic(@RequestParam int tid,
                                            @RequestAttribute(Const.VERIFY_USER_ID) int id){
        return RestBean.success(topicService.getTopic(tid,id));
    }
    @GetMapping("/interact")
    public RestBean<Void> interact(@RequestParam @Min(0) int tid,
                                    @RequestParam @Pattern(regexp = "like|collect") String type,
                                    @RequestParam boolean status,
                                    @RequestAttribute(Const.VERIFY_USER_ID) int id){
        log.info("Received parameters: tid={}, type={}, status={}, id={}", tid, type, status, id);
        topicService.Interact(new Interact(tid,id,new Date(),type),status);
        return RestBean.success();     }
    @GetMapping("/collects")
    public RestBean<List<TopicPreviewVo>> getCollects(
                                                       @RequestAttribute(Const.VERIFY_USER_ID) int id){
        log.info("Received parameters: id={}", id);
        return RestBean.success(topicService.getCollects(id));
    }
    @PostMapping("/update-topic")
    public RestBean<Void> updateTopic(@RequestBody UpdateTopicVo vo,
                                      @RequestAttribute(Const.VERIFY_USER_ID) int id){
        return utils.handleMessage(() -> (topicService.topicUpdate(id,vo)));
    }
    @PostMapping("/add-comment")
    public RestBean<Void> addComment(@RequestBody CommentVo vo,
                                      @RequestAttribute(Const.VERIFY_USER_ID) int id){
        return utils.handleMessage(() -> (topicService.addComment(id,vo)));
    }
    @GetMapping("/get-comment")
    public RestBean<List<TopicComment>> getComment(@RequestParam int  tid,
                                                   @RequestParam int page
                                                   ){
        return RestBean.success(topicService.getComment(tid,page+1));
    }
    @GetMapping("/delete-comment")
    public RestBean<Void> deleteComment(@RequestParam int  id,
                                        @RequestAttribute(Const.VERIFY_USER_ID)int uid){
        return RestBean.success(topicService.deleteComment(id,uid));
    }
}
